package org.mtmi.ui.scenes;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.mtmi.ui.GraphicUtil;
import org.mtmi.ui.M;
import org.mtmi.ui.actions.Action;
import org.mtmi.ui.actions.IAction;
import org.mtmi.ui.actions.IActionListener;
import org.mtmi.ui.controls.listeners.PopupListener;
import org.mtmi.ui.controls.popups.AbstractButtonPopup;
import org.mtmi.ui.drawables.IDrawable;

/**
 * a scene side button that fire an action when pressed.
 * 
 */
public class GlobalSceneSideActionButton extends AbstractGlobalSceneSideButton
  implements IActionListener
{

	private IAction action;
	
	/** create edit scene button, register it in scene manager listener */
	public GlobalSceneSideActionButton( Scene scene, IAction a, int flags ) {
		super(scene, flags|M.SCENE_EDITOR_CONTROL );
		action = a;
		if( action instanceof Action ) {
			((Action)a).addActionListener( this );
		}
		setCanRemoveButton( true );
	}
	
	/** @return action currently associated to this button */
	public IAction getAction() { return action; }
	
	@Override
	protected void doAction() {
		if( getScene().isEditing() ) {
			//change action ?
			final ChooseSideButtonActionPopup p = new ChooseSideButtonActionPopup( SceneManager.getDefault().getScene(), M.SCENE_EDITOR_CONTROL );
			p.addResponseListener( new PopupListener() {
				@Override
				public void popupButtonSelected(int id) {
					if( id == AbstractButtonPopup.OK ) {
						action = p.getSelectedAction();
						redraw();
					}
				}
			});
		} else {
			if( action!=null ) {
				action.run();
			}
		}
	}
	
	@Override
	public void paint(GC gc) {
		super.paint(gc);
		
		if( action!=null ) {
			IDrawable d = action.getImage();
			if( d!=null ) {
				int st = IDrawable.NORMAL;
				if( action.isEnabled() ) {
					if( isTapped ) st = IDrawable.PRESSED;
				} else {
					st = IDrawable.DISABLED;
				}
				//bounds to paint drawable depends on button shape:
				int f = getFlags() & M.DIRECTION_MASK;
				Rectangle r = new Rectangle(0,0,0,0);
				int m = 4;
				int R = Math.round( 0.707f*ER );
				if( (f & M.TOP)==M.TOP) {
					if( (f & M.LEFT )==M.LEFT ) { //top left
						GraphicUtil.setRect( r, location.x+m, location.y+m, R-m, R-m );
					} else if( (f & M.RIGHT)==M.RIGHT ) { //top right
						GraphicUtil.setRect( r, location.x+size.x-R, location.y+m, R-m, R-m );
					} else { //top button
						GraphicUtil.setRect( r, location.x+size.x/2-R, location.y+m, 2*R, R-m );
					}
				}
				else if( (f & M.BOTTOM )==M.BOTTOM ) {
					if( (f & M.LEFT )==M.LEFT ) { //bottom left
						GraphicUtil.setRect( r, location.x+m, location.y+size.y-R, R-m, R-m );
					} else if( (f & M.RIGHT)==M.RIGHT ) { //bottom right
						GraphicUtil.setRect( r, location.x+size.x-R, location.y+size.y-R, R-m, R-m );
					} else { //top button
						GraphicUtil.setRect( r, location.x+size.x/2-R, location.y+size.y-R, 2*R, R-m );
					}
				}
				else {
					if( (f & M.LEFT )==M.LEFT ) { //left side
						GraphicUtil.setRect( r, location.x+m, location.y+size.y/2-R, R-m, 2*R );
					} else if( (f & M.RIGHT)==M.RIGHT ) { //right side
						GraphicUtil.setRect( r, location.x+size.x-R, location.y+size.y/2-R, R-m, 2*R );						
					}
				}
				Point sz = d.computeSize( r.width, r.height );
				int x = r.x+(r.width-sz.x)/2;
				int y = r.y+(r.height-sz.y)/2;
				d.paint( gc, x, y, sz.x, sz.y, st ); 
			}
		}
	}

	@Override
	public void actionChanged( IAction _action )
	{
		redraw();
	}

}
